package day9.homeWork;

import java.util.ArrayList;
import java.util.List;

//a.实现一个多线程统计出1-100之间  2、3、5的倍数有哪些?
//   线程1先展示一共2的倍数有哪些
//   线程2再展示一共3的倍数有哪些
//   线程3最后展示一共5的倍数有哪些
public class DemoA implements Runnable {
    int id;
    Thread t;

    public DemoA(int id, Thread t) {
        this.id = id;
        this.t = t;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new DemoA(2, null), "2");
        Thread t2 = new Thread(new DemoA(3, t1), "3");
        Thread t3 = new Thread(new DemoA(5, t2), "5");
        t1.start();
        t2.start();
        t3.start();
    }

    @Override
    public void run() {
        if (t != null) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String name = Thread.currentThread().getName();
        List<Integer> list = new ArrayList<>();
        synchronized (DemoA.class) {
            for (int i = 1; i <= 100; i++) {
                if (i % id == 0) {
                    list.add(i);
                }
            }
            System.out.println(name + "的倍数有:" + list);
            DemoA.class.notifyAll();
        }
    }
}
